import { storageGenerator } from '@/utils/cache';
import { defineStore } from 'pinia';

export const useApplicationStore = defineStore('application', {
  state: () => {
    return {
      appData: <any>{},
      componentData: <any>{},
      expandedId: <string>'', // 离开页面时展开的节点
      selectedId: <string>'', // 离开页面时选中的节点
    };
  },
  getters: {
    getAppData(): any {
      return this.appData ?? null;
    },
    getComponentData(): any {
      return this.componentData ?? null;
    },
  },
  actions: {
    setAppData(info: any) {
      this.appData = { ...info };
    },
    setComponentData(info: any) {
      this.componentData = { ...info };
    },
    setExpandedId(str: string) {
      this.expandedId = str;
    },
    setSelectedId(str: string) {
      this.selectedId = str;
    },
  },
  // 配置是否存入本地缓存，默认存入sessionStorage，具体使用参考文档 https://seb-l.github.io/pinia-plugin-persist/
  persist: {
    enabled: true,
    strategies: [{ storage: storageGenerator('mystore'), paths: ['count'] }],
  },
});
